package com.iot08.api.service;

import com.iot08.api.constant.Constant;
import com.iot08.api.response.PxAdversterConfigurationResp;
import com.iot08.api.vo.request.PxAdversterConfigurationQueryReqVo;
import com.iot08.common.entity.app.PxAdverster;
import com.iot08.common.entity.app.PxAdversterSlot;
import com.iot08.common.entity.app.PxAdvertConfiguration;
import com.iot08.common.entity.app.VipMember;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/4/14 11:47
 **/
@Slf4j
@Service
public class PxAdversterConfigurationService extends BaseService<PxAdvertConfiguration> {

    @Resource
    private PxAdversterSlotService pxAdversterSlotService;

    @Resource
    private PxAdversterService pxAdversterService;

    @Resource
    private VipMemberService vipMemberService;

    /**
     * @description
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/4/14 14:22
     **/
    public ResponseMap queryAdConfigurationByAdSlot(PxAdversterConfigurationQueryReqVo vo) {
        PxAdversterSlot pxAdversterSlot = pxAdversterSlotService.queryByCode(vo.getSlotCode());
        ResponseMap res = ResponseMap.success();
        if(ObjectUtils.isEmpty(pxAdversterSlot)) {
            return res;
        }
        PxAdverster pxAdverster;
        if(Integer.valueOf(1).equals(vo.getIsOnlyMyAd())) {
            pxAdverster = pxAdversterService.queryAdversterByShortName(Constant.APP_NAME);
        } else {
            pxAdverster = pxAdversterService.queryAdversterByWeightPriority();
        }
        if(ObjectUtils.isEmpty(pxAdverster)) {
            return res;
        }
        Query query;
        VipMember vipMember = vipMemberService.queryVipMemberByOpenid(vo.getOpenid());
        if(ObjectUtils.isEmpty(vipMember)) {
            query = new Query(Criteria.where("adversterId").is(pxAdverster.getId()).
                    and("adversterSlotId").is(pxAdversterSlot.getId()).
                    and("status").is(1)
                    .and("userType").in(0,2));
        } else {
            query = new Query(Criteria.where("adversterId").is(pxAdverster.getId()).
                    and("adversterSlotId").is(pxAdversterSlot.getId()).
                    and("status").is(1).
                    and("userType").in(1,2));
        }
        List<PxAdversterConfigurationResp> pxAdvertConfigurations = mongoTemplate.find(query,PxAdversterConfigurationResp.class,"px_advert_configuration");
        if(!CollectionUtils.isEmpty(pxAdvertConfigurations)) {
            res.put("firstShowInterval",pxAdversterSlot.getFirstShowInterval());
            res.put("showInterval",pxAdversterSlot.getShowInterval());
            res.put("maxFrequency",pxAdversterSlot.getMaxFrequency());
            res.put("pxAdvertConfigurations",pxAdvertConfigurations);
        }
        return res;
    }
}
